package com.zut.controller;

import com.github.pagehelper.PageInfo;
import com.zut.domain.Coach;
import com.zut.domain.Evaluation;
import com.zut.domain.Exam;
import com.zut.domain.Practise;
import com.zut.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private ExamService examService;

    /*得到所有的考试信息*/
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Exam> examPageInfo = examService.getAll(pageNum, pageSize);
        Integer code = examPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = examPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, examPageInfo, msg);
    }

    /*创建考试信息*/
    @PostMapping
    public Result insert(@RequestBody Exam exam) {
        boolean flag = examService.insert(exam);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /*根据考试id删除考试信息*/
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        boolean flag = examService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*修改考试信息*/
    @PutMapping
    public Result Update(@RequestBody Exam exam) {
        boolean flag = examService.update(exam);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据场地查询练车信息*/
    @GetMapping("/place/{pageNum}/{pageSize}/{place}")
    public Result getByPlace(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String place) {
        PageInfo<Exam> practises = examService.getByPlace(pageNum, pageSize, place);
        Integer code = practises.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = practises.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, practises, msg);
    }

    /*根据科目获取考场信息
    科目一: /subject/1/1/10
    科目二: /subject/2/1/10
    科目三: /subject/3/1/10
    科目四: /subject/4/1/10
    subject为字符串
    */
    @GetMapping("/subject/{subject}/{pageNum}/{pageSize}")
    public Result getBySubject(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable Integer subject) {
        String s = String.valueOf(subject);
        PageInfo<Exam> practises = examService.getBySubject(pageNum, pageSize, s);
        Integer code = practises.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = practises.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, practises, msg);
    }
    /*根据学员id获取所能参加科目的考场信息*/
    @GetMapping("/id/{pageNum}/{pageSize}/{id}/{date}")
    public Result getExamById(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String id,@PathVariable String date) {
        PageInfo<Exam> orderExam = examService.getOrderExamPage(pageNum, pageSize, id);
        if(orderExam.getSize() != 0 ){
            Integer code = orderExam.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
            String msg = orderExam.getSize() != 0 ? "已预约" : "数据查询失败，请重试！";
            return new Result(code, orderExam, msg);
        }
        PageInfo<Exam> examPageInfo = examService.getExamById(pageNum, pageSize, id,date);
        Integer code = examPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = examPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, examPageInfo, msg);
    }

    /*根据考试exam_result id获取考场信息*/
    @GetMapping("result/{id}")
    public Result SelectById(@PathVariable String id) {
        Exam exam = examService.getByExamId(id);
        Integer code = exam != null ? Code.GET_OK : Code.GET_ERR;
        String msg = exam != null ? "" : "数据查询失败，请重试！";
        return new Result(code, exam, msg);
    }

    /*根据用户id获取预约的考试信息*/
    @GetMapping("/order/{id}")
    public Result getOrderExam(@PathVariable String id){
        Exam orderExam = examService.getOrderExam(id);
        Integer code = orderExam != null ? Code.GET_OK : Code.GET_ERR;
        String msg = orderExam != null ? "" : "数据查询失败，请重试！";
        return new Result(code, orderExam, msg);
    }

    /*根据考场 id获取考场信息*/
    @GetMapping("/{id}")
    public Result SelectByExamId(@PathVariable String id) {
        Exam exam = examService.getExam(id);
        Integer code = exam != null ? Code.GET_OK : Code.GET_ERR;
        String msg = exam != null ? "" : "数据查询失败，请重试！";
        return new Result(code, exam, msg);
    }
}
